package com.study.javaOther.recommend_system;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

public class OnlineRecommenderSystem {
    public static void main(String[] args) throws Exception {
        try {
            Map<Integer, ArrayList<Item>> sim_table = new HashMap<Integer, ArrayList<Item>>();
            BufferedReader reader = new BufferedReader(new FileReader(
                    "sim_table.txt"));
            ArrayList<Item> arr = new ArrayList<Item>();
            System.out.println("输入一个MovieID号码：");
            Scanner sc = new Scanner(System.in);
            int MovisID = sc.nextInt();
            while (reader.ready()) {
                arr = new ArrayList<>();
                int i = getValue(reader.readLine(), arr);
                sim_table.put(i, arr);
            }
            arr = sim_table.get(MovisID);
            Collections.sort(arr);
            int size = arr.size() > 15 ? 15 : arr.size();
            for (Item item : arr) {
                size--;
                System.out.println(item);
                if (size <= 0) {
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static int getValue(String str, ArrayList<Item> arr) {
        int movieid = 0;
        float rating = 0;
        String sim_table_str = str;

        String[] sim_table_data = sim_table_str.split("\t");
        for (int i = 0; i < sim_table_data.length - 1; i++) {
            String s = sim_table_data[i + 1];
            movieid = Integer.parseInt(s.split(",")[0]);
            rating = Float.parseFloat(s.split(",")[1]);
            arr.add(new Item(movieid, rating));
        }
        return Integer.parseInt(sim_table_data[0]);
    }
}